<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class reserve extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		
	}
	
	function index()
	{
		$this->main();	
	}
	
	function main()
	{    
                //check if the user is logged in from the session cockies and if yes will load the member page
		if ($this->session->userdata('c_logged_in'))
		{
			
			// get the queue list for cart from get queue function
			list ($reservelist) = $this->get_reservations();
			
			// check if there are movies in the queue list and if TRUE a table will be generated and if FALSE nothing will be generated
			if($reservelist != FALSE)
			{
				// generate the table with the two previous commands and the resulted queue of movies  and assign this generated table to an array
				$full_table['items'] = $reservelist->result_array();
			}
			else
			{
				// assign FALSE the movies array so the code can distinguish between if we have table or not and print the error if not
				$full_table['items'] = FALSE;
			}
                        
                        $full_table['array'] = $this->model_reserve->get_all_special();
			// load the queue viewing page to the member after inputting the full table with movies and games queues tables t print to member
			$this->load->view('view_reservation',$full_table);
			// run the search function to view the search field in the member page
		
		}
		else
		{
			// the user will be redirected in case there is no cockie or failure login
			redirect('yogette/user/login');
		}        
		
	}
        
        function cancel_reservation()
        {
          
		if ($this->session->userdata('c_logged_in'))
		{
                    $reserve_id = $this->uri->segment(3);
                    
                    $cus_id = $this->session->userdata('cus_id');
                    $cus_email = $this->session->userdata('cus_email');
                    $cus_username = $this->session->userdata('cus_username');
                    
                    $this->load->model('Model_reserve', 'reserve');
                    //get the customer movies queue, 1 as an input will means movies
                    $res_queue = $this->reserve->cancel_reservation($reserve_id);
                    if ($res_queue != false)
                    {
                         $this->my_reservations();
                    }
                    else
                    {
                        redirect('yogette/error');
                    }
                }
		else
		{
			// the user will be redirected in case there is no cockie or failure login
			redirect('yogette/user/login');
		}
        }
        
        function add_reservation()
        {
           
		if ($this->session->userdata('c_logged_in'))
		{
                    $cus_id = $this->session->userdata('cus_id');
                    
                    $this->load->model('Model_reserve', 'reserve');
                    $spcialevent = $this->input->post('spcialevent');
                    $reserve_id = $this->input->post('reservid');
                    //echo $spcialevent;
                    //echo $reserve_id;
                    $customer_block = $this->reserve->check_block($cus_id);
                  //get the customer movies queue, 1 as an input will means movies
                    if ($customer_block){
                            $this->reserve->reserve_place($reserve_id,$cus_id , $spcialevent);
                             $this->index();    
                        }
                        else
                        {
                            $this->index();  
                        }
                    
                }
		else
		{
                    // the user will be redirected in case there is no cockie or failure login
                    redirect('yogette/user/login');
		}
        }
        
        function add_new_reservation()
        {
           
		if ($this->session->userdata('e_logged_in'))
		{
                    $emp_email = $this->session->userdata('emp_email');
                    
                    $this->form_validation->set_rules('specialdesc', 'Special Description', 'trim|max_length[50]|xss_clean');
                    $this->form_validation->set_rules('special_date', 'Special Reservation Date',  'required|exact_length[10]|callback_check_time');
                    
                    $this->form_validation->set_rules('reserv_time_start', 'Special Reservation Start time', 'required');
                    $this->form_validation->set_rules('reserv_time_end', 'Special Reservation End time', 'required|callback_check_min');
                    
                if ($this->form_validation->run() == FALSE) {
                        // viewing the registration page to the user  
                        $this->load->view('view_add_new_reserv');
                    } else {
                        // posting all inputs to the variables  after successful validation for these inputs
                        $specialdesc = $this->input->post('specialdesc');
                        $special_date = $this->input->post('special_date');//special_date
                        $reserv_time_start = $this->input->post('reserv_time_start');
                        $reserv_time_end = $this->input->post('reserv_time_end');
                        
                       if ($this->model_reserve->add_reserve($specialdesc,  
                                $special_date, $reserv_time_start, $reserv_time_end, $emp_email)) 
                            {
                              
                                redirect('yogette/member');
                            } else {
                                redirect('reserv/add_new_reservation');       
                            }
                    }
                    
                }
		else
		{
                    // the user will be redirected in case there is no cockie or failure login
                    redirect('yogette/user/login');
		}
        }
        
        function check_min()
        {
             $special_date = $this->input->post('special_date');
             $reserv_time_start = $this->input->post('reserv_time_start');
             $reserv_time_end = $this->input->post('reserv_time_end');
              
            // this request to set a message for validation for the user in case if the email already exists in DB 
            $this->form_validation->set_message('check_min', 'This %s should be 1 hour in Schedule');                
 
            $startTime = explode(":", $reserv_time_start);
            $EndTime = explode(":", $reserv_time_end);
            
           // echo $startTime[0];
            //echo $EndTime[0];
            if ((int)$startTime[0] !== (int)$EndTime[0])
            { 
                $test = (int)$EndTime[0] - (int)$startTime[0];
                $test1 = $test * 60;
                //echo $test;
                if( $test > 1 ) // more than one hour
                {
                    return true;
                }
                else // for one hour check
                {
                   
                        $total = $EndTime[1] - $startTime[1];         
                        $last = $test1 + $total ; //add minutes witht he hours
                        if ($last > 60 || $total == 0)
                        {
                            return true;
                        }
                        else
                        {
                           return false;
                        }
                }
            }
            else
            {    return false;
               
            }
        }
        
        function check_time()
        {
             $special_date = $this->input->post('special_date');
             $reserv_time_start = $this->input->post('reserv_time_start');
             $reserv_time_end = $this->input->post('reserv_time_end');
              
            // this request to set a message for validation for the user in case if the email already exists in DB 
            $this->form_validation->set_message('check_time', 'This %s Has Conflict in Schedule');                
 
            $result = $this->model_reserve->check_exist_reservation($special_date,$reserv_time_start , $reserv_time_end);

            if ($result) {
                return false;
            } else {
               return true;
            }

        }        
        function logout()
	{
		/* to destroy the seesion and logout*/
		$this->session->sess_destroy();
                redirect('yogette/reserve');
	}
        
        function get_reservations() // viewcart function zoms
	{
            //check if the user is logged in from the session cockies and if yes will load the member page
		if ($this->session->userdata('c_logged_in'))
		{
                    //get the the customer id from the session cockie
                    $cus_id = $this->session->userdata('cus_id');
                    $cus_email = $this->session->userdata('cus_email');
                    $cus_username = $this->session->userdata('cus_username');
                    
                    $this->load->model('Model_reserve', 'reserve');
                    //get the customer movies queue, 1 as an input will means movies
                    $res_queue = $this->reserve->search_reservations();

                    // return both the movies and games queue list
                    return array ($res_queue);
                }
		else
		{
			// the user will be redirected in case there is no cockie or failure login
			redirect('yogette/user/login');
		}
	}
        
        function my_reservations() // viewcart function zoms
	{
            //check if the user is logged in from the session cockies and if yes will load the member page
		if ($this->session->userdata('c_logged_in'))
		{
                    //get the the customer id from the session cockie
                    $cus_id = $this->session->userdata('cus_id');
                    $cus_email = $this->session->userdata('cus_email');
                    $cus_username = $this->session->userdata('cus_username');
                    
                    $this->load->model('Model_reserve', 'reserve');
                    //get the customer movies queue, 1 as an input will means movies
                    $res_queue['items'] = $this->reserve->get_my_reservations($cus_id);

                    // return both the movies and games queue list
                 
                    $this->load->view('view_my_reservation',$res_queue);
                }
		else
		{
			// the user will be redirected in case there is no cockie or failure login
			redirect('yogette/user/login');
		}
	}
        
        function view_shipped_order() // viewcart function zoms
	{
            //check if the user is logged in from the session cockies and if yes will load the member page
		if ($this->session->userdata('e_logged_in'))
		{
                    //get the the customer id from the session cockie
                    $emp_id = $this->session->userdata('emp_id');
                    $emp_email = $this->session->userdata('emp_email');
                    $emp_username = $this->session->userdata('emp_username');
                    
                    //$this->load->model('Model_user', 'user');
                    //get the customer movies queue, 1 as an input will means movies
                    //$res_queue = $this->user->search_shipped_orders();

                    // return both the movies and games queue list
                    $res_queue['items'] = FALSE;
                    $this->load->view('view_shipped_order',$res_queue);
                }
		else
		{
			// the user will be redirected in case there is no cockie or failure login
			redirect('yogette/user/login');
		}
	}
        
        
        function view_emp_reservation() // viewcart function zoms
	{
            //check if the user is logged in from the session cockies and if yes will load the member page
		if ($this->session->userdata('e_logged_in'))
		{
                   
                    //$this->load->model('Model_user', 'user');
                    //get the customer movies queue, 1 as an input will means movies
                    //$res_queue = $this->user->search_shipped_orders();

                    // return both the movies and games queue list
                    $res_queue['items'] = FALSE;
                    $this->load->view('view_emp_reservation',$res_queue);
                }
		else
		{
			// the user will be redirected in case there is no cockie or failure login
			redirect('yogette/user/login');
		}
	}
}